package com.xiaobu.chart;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.MultipleCategorySeries;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

public abstract class AbstractChart implements IChart
{
	protected XYMultipleSeriesDataset buildBarDataset(String[] arrayOfTitle, List<double[]> coordinate)
	{
		XYMultipleSeriesDataset xyMultipleSeriesDataset = new XYMultipleSeriesDataset();
		
		for (int index = 0; index < arrayOfTitle.length; index++)
		{
			CategorySeries categorySeries = new CategorySeries(arrayOfTitle[index]);
			double[] arrayOfDouble = (double[])coordinate.get(index);
			
			for (int length = 0; length < arrayOfDouble.length; length++)
			{
				categorySeries.add(arrayOfDouble[length]);
			}
			
			xyMultipleSeriesDataset.addSeries(categorySeries.toXYSeries());
		}
		
		return xyMultipleSeriesDataset;
	}
	
	protected XYMultipleSeriesRenderer buildBarRenderer(int[] arrayOfColor, boolean bLargeScreen)
	{
		XYMultipleSeriesRenderer xyMultipleSeriesRenderer = new XYMultipleSeriesRenderer();
		
		if (bLargeScreen)
	    {
			xyMultipleSeriesRenderer.setMargins(new int[] { 20, 50, 50, 10});
			xyMultipleSeriesRenderer.setAxisTitleTextSize(20.0F);
			xyMultipleSeriesRenderer.setChartTitleTextSize(22.0F);
			xyMultipleSeriesRenderer.setLabelsTextSize(18.0F);
			xyMultipleSeriesRenderer.setLegendTextSize(22.0F);
	    }
		else
		{
			xyMultipleSeriesRenderer.setMargins(new int[] {10, 30, 0, 10});
			xyMultipleSeriesRenderer.setAxisTitleTextSize(11.0F);
			xyMultipleSeriesRenderer.setChartTitleTextSize(12.0F);
			xyMultipleSeriesRenderer.setLabelsTextSize(11.0F);
			xyMultipleSeriesRenderer.setLegendTextSize(11.0F);
		}
		
	    for (int index = 0; index < arrayOfColor.length; index++)
	    {
	    	SimpleSeriesRenderer simpleSeriesRenderer = new SimpleSeriesRenderer();
	    	simpleSeriesRenderer.setColor(arrayOfColor[index]);
	    	xyMultipleSeriesRenderer.addSeriesRenderer(simpleSeriesRenderer);
	    }
	    
	    return xyMultipleSeriesRenderer;
	}
	
	protected CategorySeries buildCategoryDataset(String category, double[] arrayOfDouble)
	{
		CategorySeries categorySeries = new CategorySeries(category);
		
		for (int index = 0; index < arrayOfDouble.length; index++)
		{
			double value = arrayOfDouble[index];
			
			StringBuilder stringBuilder = new StringBuilder("Project ");
			stringBuilder.append(index + 1);
			
			categorySeries.add(stringBuilder.toString(), value);
		}
		
		return categorySeries;
	}
	
	protected DefaultRenderer buildCategoryRenderer(int[] arrayOfColor, boolean bLargeScreen)
	{
		DefaultRenderer defaultRenderer = new DefaultRenderer();
		
		if (bLargeScreen)
		{
			defaultRenderer.setMargins(new int[] {20, 50, 50, 10});
			defaultRenderer.setLabelsTextSize(20.0F);
			defaultRenderer.setLegendTextSize(18.0F);
		}
		else
		{
			defaultRenderer.setMargins(new int[] {10, 30, 0, 10});
			defaultRenderer.setLabelsTextSize(11.0F);
			defaultRenderer.setLegendTextSize(11.0F);
		}
		
		for (int index = 0; index < arrayOfColor.length; index++)
		{
			SimpleSeriesRenderer simpleSeriesRenderer = new SimpleSeriesRenderer();
			simpleSeriesRenderer.setColor(arrayOfColor[index]);
			defaultRenderer.addSeriesRenderer(simpleSeriesRenderer);
		}
		
		return defaultRenderer;
	}
	
	protected XYMultipleSeriesDataset buildDataset(String[] arrayOfTitle, List<double[]> xCoordinate, List<double[]> yCoordinate)
	{
		XYMultipleSeriesDataset xyMultipleSeriesDataset = new XYMultipleSeriesDataset();
		
		for (int index = 0; index < arrayOfTitle.length; index++)
		{
			XYSeries xySeries = new XYSeries(arrayOfTitle[index]);
			
			double[] xArrayOfDouble = (double[])xCoordinate.get(index);
			double[] yArrayOfDouble = (double[])yCoordinate.get(index);
			
			for (int length = 0; length < xArrayOfDouble.length; length++)
			{
				xySeries.add(xArrayOfDouble[length], yArrayOfDouble[length]);
			}
			
			xyMultipleSeriesDataset.addSeries(xySeries);
		}
		
		return xyMultipleSeriesDataset;
	}
	
	protected XYMultipleSeriesDataset buildDateDataset(String[] arrayOfTitle, List<Date[]> xCoordinate, List<double[]> yCoordinate)
	{
		XYMultipleSeriesDataset xyMultipleSeriesDataset = new XYMultipleSeriesDataset();
		
		for (int index = 0; index < arrayOfTitle.length; index++)
		{
			TimeSeries timeSeries = new TimeSeries(arrayOfTitle[index]);
			
			Date[] arrayOfDate = (Date[])xCoordinate.get(index);
			double[] arrayOfDouble = (double[])yCoordinate.get(index);
			
			for (int length = 0; length < arrayOfDate.length; length++)
			{
				timeSeries.add(arrayOfDate[length], arrayOfDouble[length]);
			}
			
			xyMultipleSeriesDataset.addSeries(timeSeries);
		}
		
		return xyMultipleSeriesDataset;
	}
	
	protected MultipleCategorySeries buildMultipleCategoryDataset(String title, List<String[]> xCoordinate, List<double[]> yCoordinate)
	{
		MultipleCategorySeries multipleCategorySeries = new MultipleCategorySeries(title);
		
		int index = 0;
		Iterator<double[]> iterator = yCoordinate.iterator();
		
		while (true)
		{
			if (!iterator.hasNext())
			{
				break;
			}
			
			double[] arrayOfDouble = (double[])iterator.next();
			multipleCategorySeries.add(String.valueOf(index + 2007), (String[])xCoordinate.get(index), arrayOfDouble);
			index++;
		}
		
		return multipleCategorySeries;
	}
	
	protected XYMultipleSeriesRenderer buildRenderer(int[] arrayOfColor, PointStyle[] arrayOfPointStyle, Boolean bLargeScreen)
	{
		XYMultipleSeriesRenderer xyMultipleSeriesRenderer = new XYMultipleSeriesRenderer();
		
		if (bLargeScreen.booleanValue())
		{
			xyMultipleSeriesRenderer.setMargins(new int[] {20, 50, 50, 10});
			xyMultipleSeriesRenderer.setAxisTitleTextSize(20.0F);
			xyMultipleSeriesRenderer.setChartTitleTextSize(22.0F);
			xyMultipleSeriesRenderer.setLabelsTextSize(18.0F);
			xyMultipleSeriesRenderer.setLegendTextSize(22.0F);
			xyMultipleSeriesRenderer.setPointSize(7.0F);
		}
		else
		{
			xyMultipleSeriesRenderer.setMargins(new int[] {10, 30, 0, 10});
			xyMultipleSeriesRenderer.setAxisTitleTextSize(11.0F);
			xyMultipleSeriesRenderer.setChartTitleTextSize(12.0F);
			xyMultipleSeriesRenderer.setLabelsTextSize(11.0F);
			xyMultipleSeriesRenderer.setLegendTextSize(11.0F);
		}
		
		for (int index = 0; index < arrayOfColor.length; index++)
		{
			XYSeriesRenderer xySeriesRenderer = new XYSeriesRenderer();
			
			xySeriesRenderer.setColor(arrayOfColor[index]);
			xySeriesRenderer.setPointStyle(arrayOfPointStyle[index]);
			
			xyMultipleSeriesRenderer.addSeriesRenderer(xySeriesRenderer);
		}
		
		return xyMultipleSeriesRenderer;
	}
	
	protected void setChartSettings(XYMultipleSeriesRenderer xyMultipleSeriesRenderer,
			String title, String xTitle, String yTitle,
			double xAxisMin, double xAxisMax, double yAxisMin, double yAxisMax,
			int axesColor, int labelsColor)
	{
		xyMultipleSeriesRenderer.setChartTitle(title);
		xyMultipleSeriesRenderer.setXTitle(xTitle);
		xyMultipleSeriesRenderer.setYTitle(yTitle);
		xyMultipleSeriesRenderer.setXAxisMin(xAxisMin);
		xyMultipleSeriesRenderer.setXAxisMax(xAxisMax);
		xyMultipleSeriesRenderer.setYAxisMin(yAxisMin);
		xyMultipleSeriesRenderer.setYAxisMax(yAxisMax);
		xyMultipleSeriesRenderer.setAxesColor(axesColor);
		xyMultipleSeriesRenderer.setLabelsColor(labelsColor);
	}
}